package com.tanhua.api.impl.mongo;

import com.tanhua.api.houtai.SoulQuestionApi;
import com.tanhua.domain.mongo.QuestionList;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

/**
 * @author user
 */
@Service
public class SoulQuestionApiImpl implements SoulQuestionApi {
    @Autowired
    public MongoTemplate mongoTemplate;


    /**
     * 根据id查询题
     *
     * @param questionId
     */
    @Override
    public QuestionList fingSoulQuestionbyId(Integer questionId) {
        Query query = new Query(Criteria.where("questionId").is(questionId.longValue()));
        QuestionList questionList = mongoTemplate.findOne(query, QuestionList.class);
        return questionList;
    }

    /**
     * 删除题
     *
     * @param questionId
     */
    @Override
    public void deleteQuestionById(Integer questionId) {
        Query query = new Query(Criteria.where("questionId").is(questionId));
        mongoTemplate.remove(query, "soul_question_list");
    }

    /**
     * 修改题
     *
     * @param questionList
     */
    @Override
    public void updateQuestion(QuestionList questionList) {
        Query query = new Query(Criteria.where("questionId").is(questionList.getQuestionId()));
        Update update = new Update();
        update.set("question", questionList.getQuestion());
        mongoTemplate.upsert(query, update, QuestionList.class);
    }

    /**
     * 添加题
     *
     * @param questionList
     */
    @Override
    public void addQuestion(QuestionList questionList) {
        mongoTemplate.insert(questionList, "soul_question_list");

    }

    /**
     * 删除选项
     *
     * @param questionList
     */
    @Override
    public void updateQption(QuestionList questionList) {
        Query query = new Query(Criteria.where("questionId").is(questionList.getQuestionId()));
        Update update = new Update();
        update.set("options", questionList.getOptions());
        mongoTemplate.upsert(query, update, QuestionList.class);
    }
}
